//
//  CSGXFootView.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/10/31.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXFootView.h"

@interface CSGXFootView ()
@property (weak, nonatomic) IBOutlet UIView *vLine;

@property (weak, nonatomic) IBOutlet UIButton *btnBalance;

@property (weak, nonatomic) IBOutlet UILabel *lblCount;

@property (weak, nonatomic) IBOutlet UILabel *lblTotalPrice;
@property (weak, nonatomic) IBOutlet UILabel *lblSaleType;
/// 结算按钮
- (IBAction)btnBalance:(id)sender;

@end

@implementation CSGXFootView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.btnBalance.backgroundColor = kColorBar;
    self.lblCount.textColor = [UIColor orangeColor];
    self.lblTotalPrice.textColor = [UIColor orangeColor];
    self.lblSaleType.textColor = [UIColor grayColor];
}

#pragma mark - Actions
- (IBAction)btnBalance:(id)sender {
    
    if (self.callBackBlock_FootView) {
        self.callBackBlock_FootView();
    }
}


#pragma mark - methods

- (void)distributeWithDict:(NSDictionary *)dict {
    //@{@"unitCount":unitCount, @"money":money, @"typeMoney":typeMoney};
    self.lblCount.attributedText = (NSAttributedString *)dict[@"unitCount"];
    self.lblTotalPrice.attributedText = dict[@"money"];
    self.lblSaleType.text = dict[@"typeMoney"];
}

//- (NSDictionary *)getDictWithSaleList:(NSArray<TSaleInfoModel *> *)saleList {
- (NSDictionary *)getFooterDictWithOrderModel:(TOrderInfoModel *)orderModel {
    
    NSArray *countArr = [orderModel.orCount componentsSeparatedByString:@","];
    
    NSString *unitCount = [NSString stringWithFormat:@"%@大 %@中 %@小", countArr[0], countArr[1], countArr[2]];
    NSString *totalMoney = [NSString stringWithFormat:@"￥%.2f", orderModel.orTotalMoney];
    NSString *typeMoney = [NSString stringWithFormat:@"销%@ - 退%.2f", totalMoney, 0.00];
    
    NSAttributedString *unitCountAttr = [self getUnitCountAttrStr:unitCount];
    NSAttributedString *totalMoneyAttr = [self getTotalMoneyAttrStr:totalMoney];

    return @{@"unitCount":unitCountAttr, @"money":totalMoneyAttr, @"typeMoney":typeMoney};
}

- (NSAttributedString *)getUnitCountAttrStr:(NSString *)str {
    NSRange rangeBig = [str rangeOfString:@"大"];
    NSRange rangeMid = [str rangeOfString:@"中"];
    NSRange rangeSma = [str rangeOfString:@"小"];
    
    NSRange tarRangeBig = NSMakeRange(0, rangeBig.location);
    NSRange tarRangeMid = NSMakeRange(rangeBig.location + rangeBig.length, rangeMid.location - (rangeBig.location + rangeBig.length));
    NSRange tarRangeSma = NSMakeRange(rangeMid.location + rangeMid.length, rangeSma.location - (rangeMid.location + rangeMid.length));
    
    NSMutableAttributedString * mAttrStr = [[NSMutableAttributedString alloc] initWithString:str];
    [mAttrStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} range:tarRangeBig];
    [mAttrStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} range:tarRangeMid];
    [mAttrStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} range:tarRangeSma];
    
    return mAttrStr;
}

- (NSAttributedString *)getTotalMoneyAttrStr:(NSString *)str {
    NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:str];
    [mAttrStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} range:NSMakeRange(0, 1)];
    return mAttrStr;
}

#pragma mark - setter & getter
- (void)setPrevFooterOrderModel:(TOrderInfoModel *)prevFooterOrderModel {
    _prevFooterOrderModel = prevFooterOrderModel;
    
    NSDictionary *dict = [self getFooterDictWithOrderModel:prevFooterOrderModel];
    [self distributeWithDict:dict];
}

@end
